package com.example.administrator.gridviewactivity;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridviewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);
        setTitle("网络视图");
        GridView gv=(GridView)findViewById(R.id.grid_view);
        gv.setAdapter(new ImageAdapter(this));
    }
    public class ImageAdapter extends BaseAdapter{
        private Context mContext;
        public ImageAdapter(Context c){
            mContext=c;
        }

        private Integer[] mThumbIds={
                R.drawable.grid_view01,R.drawable.grid_view02,
                R.drawable.grid_view03,R.drawable.grid_view04,
                R.drawable.grid_view05,R.drawable.grid_view06,
                R.drawable.grid_view07,R.drawable.grid_view08,
                R.drawable.grid_view09,R.drawable.grid_view10,
                R.drawable.grid_view11,R.drawable.grid_view12,
                R.drawable.grid_view13,R.drawable.grid_view14,
                R.drawable.grid_view15,R.drawable.sample_1,
                R.drawable.sample_2,R.drawable.sample_1,
                R.drawable.sample_4,R.drawable.sample_5,
                R.drawable.sample_6,R.drawable.sample_7
        };
        public int getCount(){
            return mThumbIds.length;
        }
        public Object getItem(int arg0){
            return null;
        }
        public long getItemId(int arg0){
            return 0;
        }
        public View getView(int position,View convertView, ViewGroup parent){
            ImageView imageView;
            if(convertView==null){
                imageView=new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85,85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8,8,8,8);
            }
            else{
                imageView=(ImageView)convertView;
            }
            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }
    }

}
